package com.ezcoding.engine;

import com.ezcoding.model.*;
import com.ezcoding.utils.StatusUtil;
import com.ezcoding.utils.Tools;

import java.util.List;
import java.util.Map;

public class SaleBackCalculator extends BaseDingdanCalculator {

    public SaleBackCalculator(Dingdan dingdan, List<DingdanDetail> details) {
        super(dingdan, details);
    }

    @Override
    public boolean checkProductStore(int curCount, int needCount) {
        return true;
    }

    @Override
    public double getCurTicket(double curValue) {
        if (dingdan.getIsTicket() == 1) {
            //不开票
            return 0;
        }

        return curValue * -1;
    }

    @Override
    public double getCurMoney(double curValue) {
        return curValue * -1;
    }

    @Override
    public void setDetailTicket(DingdanDetail detail, double value) {
        detail.setTicketOut(value);
    }

    @Override
    public void setDetailMoney(DingdanDetail detail, double value) {
        detail.setMoneyIn(value);
    }

    @Override
    public void setTicket(double value) {
        dingdan.setTicketOut(value);
    }

    @Override
    public void setMoney(double value) {
        dingdan.setMoneyIn(value);
    }

    @Override
    public double getTotalMoney() {
        return -dingdan.getSourceFreightUnpaid() + dingdan.getTargetFreightPaid() + dingdan.getFreeMoney() + dingdan.getMoney();
    }

    @Override
    public void updateCompanyInfo() {
        Company company = getCompany();

        company.setTotalTicket(company.getTotalTicket() + dingdan.getTicket());
        company.setTotalMoney(company.getTotalMoney() - dingdan.getSourceFreightPaid() - dingdan.getTargetFreightPaid() - dingdan.getMoney());
    }

    @Override
    public Map<String, Object> updateRelatedDingdanInfo() {
        Dingdan relatedDingdan = getRelatedDingdan();

        if (relatedDingdan == null) {
            return StatusUtil.getSuccessStatusWithEmptyMessage();
        }

        //结算订单为销退单，关联订单为采退单，校验票款金额和货款金额
        if (dingdan.getTicketOut().doubleValue() != relatedDingdan.getTicketIn()) {
            return StatusUtil.getFailedStatusWithMessage("票款金额不一致[订单票款：" + dingdan.getTicketOut() + "，采退单票款：" + relatedDingdan.getTicketIn() + "]");
        }

        if (dingdan.getMoneyIn().doubleValue() != relatedDingdan.getMoneyOut()) {
            return StatusUtil.getFailedStatusWithMessage("货款金额不一致[订单货款：" + dingdan.getMoneyIn() + "，采退单货款：" + relatedDingdan.getMoneyOut() + "]");
        }

        if (!deleteRelatedMessage(relatedDingdan)) {
            return StatusUtil.getFailedStatusWithMessage("删除关联消息失败！");
        }

        return StatusUtil.getSuccessStatusWithEmptyMessage();
    }

    @Override
    public int getRelationType() {
        return UserRelation.RELATION_TYPE_CLIENT;
    }

    @Override
    public void updateProductStore(Product product, int needCount) {
        product.setCount(product.getCount() + needCount);
    }

    @Override
    public void updateProductAndDetail(Product product, DingdanDetail detail) {
        updateProfit(detail, product.getInPrice());
        updateProductStore(product, detail.getCount());
    }

    @Override
    public void updateProfit(DingdanDetail detail, double inPrice) {
        double profit = (detail.getPrice() / getTaxRate() - inPrice) * detail.getCount() * -1;
        detail.setProfit(Tools.formatDouble(profit));
    }

    @Override
    public void updateProductInPrice(Product product, DingdanDetail detail) {

    }
}
